package com.android.lovely.manager

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.android.lovely.R
import com.android.lovely.data.AppConfig
import com.android.lovely.data.Language
import com.android.lovely.data.LifecycleInfo
import com.android.lovely.data.LovelyLifecycle
import com.android.lovely.data.StoreDialogueStyle
import com.android.lovely.data.UserBean
import com.android.lovely.util.MMKVChangeListener
import com.android.lovely.util.MMKVUtils
import com.android.lovely.util.registerChangeListener
import com.android.lovely.util.unregisterChangeListener
import com.kotlin.extensions.appContext
import com.kotlin.extensions.json.JsonExt.fromJson
import com.kotlin.extensions.json.JsonExt.fromJsonByType
import com.kotlin.extensions.json.JsonExt.toJson
import com.kotlin.extensions.json.JsonExt.toJsonByType
import kotlin.collections.orEmpty

/**
 *
 * @author why
 * @date 2025/3/6 15:55
 */


object KVStore {

    private val KVStore
        get() = MMKVUtils.instance.create("LOVELY_DEFAULT")

    fun Lifecycle.registerChangeListener(listener: MMKVChangeListener) {
        KVStore.registerChangeListener(listener)
        addObserver(object : DefaultLifecycleObserver {
            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                KVStore.unregisterChangeListener(listener)
            }
        })
    }

    var refers: String
        get() = KVStore.decode("install_refers", "")
        set(value) = KVStore.encode("install_refers", value)

    var token: String
        get() = KVStore.decode("loginer_token", "")
        set(value) = KVStore.encode("loginer_token", value)

    var firebase: String
        get() = KVStore.decode("firebase_token", "")
        set(value) = KVStore.encode("firebase_token", value)

    var loginer: UserBean?
        get() = KVStore.decode("loginer","").fromJson<UserBean>()
        set(value) = KVStore.encode("loginer", value.toJson())
//
    var system: AppConfig?
        get() = KVStore.decode("app_config", "")?.fromJson()
        set(value) {
            KVStore.encode("app_config", value.toJson())
        }

    var agreeLimit: Boolean
        get() = KVStore.decode("is_18_agree", false)
        set(value) {
            KVStore.encode("is_18_agree", value)
        }

    var speakLanguage: Language
        get() = KVStore.decode("speak_language", "").fromJson() ?: Language()
        set(value) {
            KVStore.encode("speak_language", value.toJson())
        }

    var lovelyLifecycle: List<LovelyLifecycle>
        get() = KVStore.decode("a9hdaLvFPRtJadge", "").fromJsonByType<List<LovelyLifecycle>>()
            .orEmpty()
        set(value) {
            KVStore.encode("a9hdaLvFPRtJadge", value.toJsonByType())
        }

    var lifecycleInfo: LifecycleInfo?
        get() = KVStore.decode("jjzKjbx2yWJPzzaV", "").fromJson()
        set(value) {
            KVStore.encode("jjzKjbx2yWJPzzaV", value.toJson())
        }

    var talkTranslate: Boolean
        get() = KVStore.decode("talk_translate", false)
        set(value) {
            KVStore.encode("talk_translate", value)
        }

    var notifyRequestShowed: Boolean
        get() = KVStore.decode("requested_notify", false)
        set(value) {
            KVStore.encode("requested_notify", value)
        }

    var showSignInTime: Long = KVStore.decode("showed_sign", 0)
        set(value) {
            field = value
            KVStore.encode("showed_sign", value)
        }

    var signedTime: String = KVStore.decode("signed_time", "")
        set(value) {
            field = value
            KVStore.encode("signed_time", value)
        }

    var noticeContent: String = KVStore.decode("notice_content", "")
        set(value) {
            field = value
            KVStore.encode("notice_content", value)
        }

    var isFirstShowBigCardGuide: Boolean
        get() = KVStore.decode("is_first_card_guide_showed", false)
        set(value) {
            KVStore.encode("is_first_card_guide_showed", value)
        }

    var enableGif: Boolean = KVStore.decode("enableGif", true)
        set(value) {
            field = value
            KVStore.encode("enableGif", value)
        }

    var hasShowNotifyPermission: Boolean = KVStore.decode("showedNotifyPermission", false)
        set(value) {
            field = value
            KVStore.encode("showedNotifyPermission", value)
        }

    var showUMP: Boolean = KVStore.decode("ump", false)
        set(value) {
            field = value
            KVStore.encode("ump", value)
        }

    var virtualShowTime: Long = KVStore.decode("UA1EgGfCmemPsisy", 0)
        set(value) {
            field = value
            KVStore.encode("UA1EgGfCmemPsisy", value)
        }

    var discountShowTime: Long = KVStore.decode("U1ixc9hpog9lkolZ", 0)
        set(value) {
            field = value
            KVStore.encode("U1ixc9hpog9lkolZ", value)
        }

    var regenerateRemind: Boolean = KVStore.decode("regenerateRemind", true)
        set(value) {
            field = value
            KVStore.encode("regenerateRemind", value)
        }

    var talkStyle: StoreDialogueStyle = KVStore.decode("talk_style", "")?.fromJson<StoreDialogueStyle>() ?: StoreDialogueStyle()
        set(value) {
            field = value
            KVStore.encode("talk_style", field.toJson())
        }

    var appLanguage: Language = KVStore.decode("app_language", "").fromJson() ?: Language(
        "en", appContext.getString(R.string.en)
    )
        set(value) {
            field = value
            KVStore.encode("app_language", value.toJson())
            LanguageManager.changeAppLanguage(appContext, value.code)
        }

    var signToday: String = KVStore.decode("sign_today", "")
        set(value) {
            field = value
            KVStore.encode("sign_today", value)
        }
    var signShowToday: String = KVStore.decode("sign_show_today", "")
        set(value) {
//            field = value
            KVStore.encode("sign_show_today", value)
        }

    var lastChatAdTime: Long = KVStore.decode("last_chat_ad_time", 0)
        set(value) {
            field = value
            KVStore.encode("last_chat_ad_time", value)
        }
}
